package Server.init;


import Server.dao.MessageDao;
import Server.dao.ShopOrderDao;
import Server.dao.ShopSpecDao;
import Server.dao.UsersDao;
import Server.entity.Message;
import Server.entity.ShopOrder;
import Server.entity.ShopSpec;
import Server.tool.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Component
@Lazy(false)
public class Timeer {
    @Autowired
    private EntityManager entityManager;
    @Autowired
    private ShopSpecDao shopSpecDao;
    @Autowired
    private MessageDao messageDao;
    @Autowired
    private ShopOrderDao shopOrderDao;
    @Autowired
    private UsersDao usersDao;
    @Scheduled(cron = "0 1 0 * * ?")
    public void logTime(){
        List<ShopSpec> lx=shopSpecDao.findByStatusAndRepertoryBetween(ShopSpec.Status.ING,0,11);
        for(ShopSpec spec :lx){
            Message message=Message.getInstance();
            message.setReadMI(0);
            message.setMessage("您的商品"+spec.getShop().getName()+"【规格："+spec.getName()+"】库存不足10");
            message.setShopSpec(spec);
            message.setUser(usersDao.findByUuid("1"));
            messageDao.save(message);
        }
        List<ShopOrder> lxs = shopOrderDao.findByStatusAndCreateTime(TimeUtils.getTimeFormat());
        for(ShopOrder item:lxs){
            item.setStatus(ShopOrder.Status.YES);
            shopOrderDao.save(item);
        }
    }
}
